function[]=ksdens(X,f)
% function ksdens(X)
% GUI for Kernel Density Estimation
% X - random sample
% f - string with the estimated density
%
% (C) Jiri Zelinka, Jan Kolacek and Michal Laska, Masaryk University, Brno, Czech Republic
%

w=which('ksdens.m');
w(end-8:end)=[];
addpath(w);
addpath([w,'/dens']);
addpath([w,'/dens/base']);
addpath([w,'/dens/functions']);
%addpath([w,'/regress']);
%addpath([w,'/quality']);
%addpath([w,'/distrib']);
%addpath([w,'/bivardens']);


% initial definitions

K=[];
hist_bits=10;
LoH=[]; % list of handles

% create the main window

CreateMainStr='save KDE_tempsavedata;clear all';
CloseMainStr='mf=findobj(''Tag'',''KDE_MAIN'');set(gcf,''CloseRequestFcn'',''closereq'');close(mf);clear all;if exist(''KDE_tempsavedata.mat'')==2,load KDE_tempsavedata;delete KDE_tempsavedata.mat;end';

main_fig=figure( ...
   'Visible','on', ...
   'Name','Kernel Density Estimate', ...
   'CreateFcn',CreateMainStr,...
   'CloseRequestFcn',CloseMainStr,...
   'Units','Normalized',...
   'NumberTitle','off',...
   'Tag','KDE_MAIN');

LoH=[LoH,main_fig];

axes('Position',[0.08 0.08 0.7 0.85]);

RightBorder=uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.85,0,.20,1], ...
        'BackgroundColor',[0.50 0.50 0.50]);

top=0.825;
%  text Data
    DataHndl1=uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.0855,.125,0.05], ...
        'BackgroundColor',[0.50 0.50 0.50], ...
        'String','Data');
%  button Data input
    DataHndl2=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.05,.125,0.05], ...
        'String','Input', ...
        'Callback','KDE_datainput');
%  button Data view
    DataHndl3=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top,.125,0.05], ...
        'String','View', ...
        'Callback','KDE_datadraw');
%  button Data save
    DataHndl4=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top-0.05,.125,0.05], ...
        'String','Save', ...
        'Callback','KDE_datasave');
LoH=[LoH,DataHndl2,DataHndl3,DataHndl4];


top=.65;
% button Histogram
    HistHndl10=uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.05,.125,0.05], ...
        'BackgroundColor',[0.50 0.50 0.50], ...
        'String','Histogram');
   HistHndl1=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top,.125,0.05], ...
        'String','View', ...
        'Callback','KDE_histdraw');
LoH=[LoH,HistHndl1];
   HistHndl2=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top-0.05,.125,0.05], ...
        'String','Parameters', ...
        'Callback','KDE_histpars');
LoH=[LoH,HistHndl2];

top=.425;
% button Setting
   SetHndl=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'FontSize',0.3,...
        'Position',[0.86,top,.125,0.1], ...
        'String','Estim.setting', ...
        'Callback','KDE_setting');
LoH=[LoH,SetHndl];

top=0.3;
%  estimates
    EstHndl1=uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.05,.125,0.05], ...
        'BackgroundColor',[0.50 0.50 0.50], ...
        'String','Estimate');
    EstHndl2=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top,.125,0.05], ...
        'String','View', ...
        'Callback','clear all;KDE_estimdraw');
    EstHndl3=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'FontSize',0.4,...
        'Position',[0.89,top-0.05,.065,0.05], ...
        'String','Bound.pars.', ...
        'Callback','KDE_bounds_par');
    EstHndl4=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top-0.05,.03,0.05], ...
        'String','L', ...
        'Callback','clear all;KDE_bounds_left');
    EstHndl5=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.955,top-0.05,.03,0.05], ...
        'String','R', ...
        'Callback','clear all;KDE_bounds_right');

LoH=[LoH,EstHndl2,EstHndl3,EstHndl4,EstHndl5];

top=.14;
% button Setting
   DrawfHndl=uicontrol( ...
        'Style','push', ...
        'Tag','KDE_DrawFunction', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'FontSize',0.3,...
        'UserData',0,...
        'Position',[0.86,top,.125,0.075], ...
        'String','Draw true density', ...
        'Callback','KDE_DrawF');
LoH=[LoH,DrawfHndl];



top=0.005;
% button Close
CloseStr='mf=findobj(''Tag'',''KDE_MAIN'');set(gcf,''CloseRequestFcn'',''closereq'');close(mf);clear all;if exist(''KDE_tempsavedata.mat'')==2,load KDE_tempsavedata;delete KDE_tempsavedata.mat;end';
mf=findobj('Tag','KDE_MAIN');
figure(mf);

    CloseHndl=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.025,.125,0.075], ...
        'String','Close', ...
        'Callback',CloseStr);


if nargin==0 % load or simulate data
  % creating data structure
  X=[];
  xx=[];
  h=[];
  f_est=[];
  bounds=[];
  f=[];
  nH=length(LoH);
  for ii=3:nH
    set(LoH(ii),'Enable','off');
  end
else % nargin>0, initial definitions
  minX=min(X);
  maxX=max(X);
  DX=maxX-minX;
  xx=linspace(minX-0.1*DX,maxX+0.1*DX,201);
%  h=h_ms(X,K);
%  f_est=K_dest(xx,X,h,K);
  h=[];
  f_est=[];
  bounds=[min(X),max(X)];
  nH=length(LoH);
  for ii=8:nH
    set(LoH(ii),'Enable','off');
  end
  if nargin==1, f=[]; 
  else
   fs=deblank(f);
   if length(fs)>0
    fs=strrep(fs,'.*','*');
    fs=strrep(fs,'./','/');
    fs=strrep(fs,'.^','^');
    fs=strrep(fs,'*','.*');
    fs=strrep(fs,'/','./');
    fs=strrep(fs,'^','.^');
    f=inline(fs);
   else f=[];
   end
  end

end % nargin>0
    
    
main_data=struct('K',K,'X',X,'xx',xx,'h',h,'f_est',f_est,'hist_bits',hist_bits,'bounds',bounds,'f',f,'LoH',LoH);
set(main_fig,'UserData',main_data);

% button print
    callprint1='curr=get(gcf,''CurrentAxes'');fig=figure;cc=copyobj(curr,fig);';
    callprint2='set(cc,''Position'',[0.08 0.08 0.85 0.85]);';
    uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.001,.96,.067,0.035], ...
        'String','Print', ...
        'Callback',[callprint1,callprint2]);
    
% button grid
    uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'FontSize',0.6,...
        'Position',[0.001,0.001,.067,0.035], ...
        'String','Grid on/off', ...
        'Callback','grid;');

set(main_fig,'Position',[0.10 0.15 0.8 0.7]);

